Vue.use(VueRouter);
var timestamp = (new Date()).getTime();
// 定义路由
var routes = [
    { path: '/',  hidden: true,redirect: '/login'},
    { path: '/login', hidden: true, component: Login,name:"登录"},
    { path: '/main', component: Main },
    {
        path: '/merchants',
        name: '商户号管理',
        component: Main,
        // iconCls: 'el-icon-star-on',
        enabeld:[admin,access],
        meta:{enabeld:[admin,access]},
        children: [
            {
                path: '/merchants/manage',
                component: Merchants,
                enabeld:[admin,access],
                name: '商户号管理'
            }
        ]
    },
    {
        path: '/system',
        name: '生活号管理',
        component: Main,
        // iconCls: 'el-icon-star-on',
        enabeld:[admin,access],
        meta:{enabeld:[admin,access]},
        children: [
            {
                path: '/system/alipay',
                component: AlipayManager,
                enabeld:[admin,access],
                name: '生活号管理'
            }
        ]
    },
    {
        path: '/userManage',
        name: '人员管理',
        component: Main,
        // iconCls: 'el-icon-user-solid',
        enabeld:['admin','alipay',access],
        children: [
            {
                path: '/userManage/alipay',
                component: AlipayManagerUser,
                enabeld:['admin',access],
                name: '代理商管理'
            },
            {
                path: '/userManage/sales',
                component: SalesUser,
                name: '话务员管理'
            }
        ]
    },
    {
        path: '/product',
        name: '商品管理',
        component: Main,
        // iconCls: 'el-icon-s-shop',
        enabeld:[admin,alipayManager,sales,access],
        children: [
            {
                path: '/product/manage',
                component: Product,
                enabeld:[admin,alipayManager,sales,access],
                name: '商品管理'
            }
        ]
    },
    {
        path: '/sales',
        name: '销售管理',
        component: Main,
        // iconCls: 'el-icon-s-marketing',
        children: [
            {
                path: '/sales/order',
                component: Order,
                name: '订单管理'
            },{
                path: '/sales/order/download',
                component: OrderDownload,
                name: '订单下载'
            },{
                path: '/sales/aliOrder',
                component: AliOrder,
                enabeld:[admin],
                name: '阿里订单'
            },{
                path: '/black',
                component: Black,
                enabeld:[admin,alipayManager],
                name: '黑名单管理'
            }
        ]
    },
    {
        path: '/QRCode',
        name: '二维码管理',
        component: Main,
        // iconCls: 'el-icon-camera-solid',
        enabeld:[admin,alipayManager,sales,access],
        children: [
            {
                path: '/QRCode/manager',
                component: QRCodePage,
                enabeld:[admin,alipayManager,sales,access],
                name: '二维码管理'
            }
        ]
    },
    {
        path: '/setting',
        name: '系统设置',
        component: Main,
        // iconCls: 'el-icon-camera-solid',
        enabeld:[admin],
        children: [
            {
                path: '/setting/logoConfig',
                component: LogoConfig,
                enabeld:[admin],
                name: '系统设置'
            }
        ]
    },
];

// 创建路由
var router = new VueRouter({
    routes:routes
});

router.beforeEach((to, from, next) => {
    //NProgress.start();
    var r = router;

    var path = to.matched?to.matched[0].path:'';
    var permission =[];
    r.options.routes.forEach(k=>{
      if(k.path == path){
    //     store.commit('setTitle', {
    //       name: k.title
    //     })
    if(k.enabeld){
        permission=k.enabeld;
    }
      }
    });
    if (to.path == '/login') {
        sessionStorage.removeItem('loginUser');
    }
    if (CommonUtils.checkNull(sessionStorage.loginUser) && to.path != '/login') {
        // document.title = "登录";
        next({ path: '/login' })
    } else {
        if(to.path != '/login'){
            if(permission.length>0){
                var json = CommonUtils.getPermissionJson();
                var flag = false;
                permission.forEach(k=>{
                    if(json[k]){
                        flag = true;
                    }
                });
                if(!flag){
                    return;
                }
            }
        }
        // document.title = to.name
        next()
    }
})

//创建和挂载根实例
var app = new Vue({
    router:router,
    store,
    el:'#app'
})

